// pages/carriage/carriage.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabArr: {
      curHdIndex: 0,
      curBdIndex: 0
    },
    navindex: 1,
    carriage:[
      { name: '闪闪', phone: '13230057427' },
      { name: '闪闪', phone: '13261051717' },
      { name: '闪闪', phone: '13214602964' },
      { name: '闪闪', phone: '13248512619' },
      { name: '闪闪', phone:'13240104185'},
    ],
    acquaintance:[
      { name: '闪闪1', phone: '1329518871' },
      { name: '闪闪2', phone: '13261915786' },
      { name: '闪闪3', phone: '13246173008' },
      { name: '闪闪4', phone: '13298148923' }, 
    ],
    chose: false,
    edit: '编辑',
  },
  //点击添加承运人
  submit(e){
    var name = e.detail.value.name;  //获取姓名值
    var phone = e.detail.value.phone;//获取电话值
    var carriage = this.data.carriage;
    console.log(carriage)
    if (!name) {
      wx.showToast({
        title: '姓名不能为空',
        icon: 'none',
      })
      return false;
    }
    if (!phone) {
      wx.showToast({
        title: '电话不能为空',
        icon: 'none',
      })
      return false;
    } if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(phone))){
      wx.showToast({
        title: '请输入正确的电话',
        icon: 'none',
      })
      return false;
    }
    for (var i = 0; i<carriage.length;i++ ){
      if (phone == carriage[i].phone){
        wx.showToast({
          title: '请勿重复添加',
          icon: 'none',
        })
        return false;
      }  
    }
    wx.showToast({
      title: '添加成功',
      icon: 'success',
      duration: 1500
    })
    var shuren = { name: name, phone: phone };  //把两个值存成变量
    var carriage = this.data.carriage;
    console.log(shuren)
    carriage.push(shuren)   //添加到数组
    this.setData({
      carriage: carriage
    });
  },
  tabFun: function (e) {
    //获取触发事件组件的dataset属性 
    var _datasetId = e.target.dataset.id;
    console.log("----" + _datasetId + "----");
    var _obj = {};
    _obj.curHdIndex = _datasetId;
    _obj.curBdIndex = _datasetId;
    this.setData({
      tabArr: _obj
    });
  },
  // 点编辑删除
  start_edit(){
    var chose = this.data.chose;
    var edit = this.data.edit;
    console.log(chose)
    if (edit == '编辑') {
      this.setData({
        chose: true,
        edit: '完成'
      })
    } else {
      this.setData({
        chose: false,
        edit: '编辑'
      })
    } 
  },
  //删除
  dalete() {
    var that = this;
    var carriage = that.data.carriage;
    console.log(carriage)
    for (var i = 0; i < carriage.length; i++) {
      if (that.data.carriage[i].status) {
        carriage.splice(i--, 1)
      }
    };
    that.setData({
      carriage: carriage,
    });
  },
  //点击选中
  chose_opt(e) {
    var index = e.currentTarget.dataset.index;
    var carriage = this.data.carriage;
    carriage[index].status = !carriage[index].status
    this.setData({
      carriage: carriage,
    })
  },
  //添加熟人点击选中
  chose_acquaintance(e) {
    var index = e.currentTarget.dataset.index;  
    var acquaintance = this.data.acquaintance;   //熟人列表
    var name = acquaintance[index].name;   //熟人姓名
    var phone = acquaintance[index].phone; //熟人电话
    acquaintance[index].status = !acquaintance[index].status;      //选中未选中
    var choseinfo = { name: name, phone: phone };   //将熟人
    console.log(acquaintance[index].status)
    var carriage = this.data.carriage;    //承运人列表
    //当选中的时候添加到承运人数组
    if (acquaintance[index].status==true){
      carriage.push(choseinfo)   //添加到数组
    }else{
      for (var i = 0; i < carriage.length; i++) {
        if (phone == carriage[i].phone) {
          carriage.splice(i, 1)
        }
      }
    }
    this.setData({
      acquaintance: acquaintance,     //选中的状态
      carriage: carriage   //承运人列表重排
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})